﻿using AutoMapper;
using Common.Utility;
using DTO;
using DTO.DTO.Com.BasicType;
using DTO.DTO.Draw.Entry;
using IServices.Com;
using IServices.Draw;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Web.Controllers;
using Web.Filter;

namespace Web.Areas.Draw.Controllers
{
    public class EntryController : BaseController
    {
        private readonly IEntryService _service;
        private readonly IBasicTypeService _basicTypeService;
        public EntryController(IHttpContextAccessor httpContextAccessor, IMapper mapper, IEntryService service, IBasicTypeService basicTypeService)
            : base(httpContextAccessor, mapper)
        {
            _service = service;
            _basicTypeService = basicTypeService;
        }
        // GET: Bus/Entry
        public async Task<IActionResult> Index()
        {
            ViewBag.Types = await _basicTypeService.GetList(new BasicTypeQueryDTO { Type = "EntryType" });
            return View();
        }

        public async Task<ResultList> List(EntryQueryDTO dto)
        {
            var result = await _service.GetPageList(dto);
            var map = _mapper.Map<List<EntryListDTO>>(result.Item1);
            var typeIds = map.Where(p => !string.IsNullOrWhiteSpace(p.Type)).Select(p => p.Type).ToList();
            typeIds.AddRange(map.Where(p => !string.IsNullOrWhiteSpace(p.Type2)).Select(p => p.Type2).ToList());
            typeIds.AddRange(map.Where(p => !string.IsNullOrWhiteSpace(p.Type3)).Select(p => p.Type3).ToList());
            var basicTypes = await _basicTypeService.GetByIds(typeIds);
            map.ForEach(item =>
            {
                if (!string.IsNullOrWhiteSpace(item.Type))
                {
                    item.TypeName = basicTypes.FirstOrDefault(p => p.Id == item.Type)?.Title ?? string.Empty;
                }
                if (!string.IsNullOrWhiteSpace(item.Type2))
                {
                    item.Type2Name = basicTypes.FirstOrDefault(p => p.Id == item.Type2)?.Title ?? string.Empty;
                }
                if (!string.IsNullOrWhiteSpace(item.Type3))
                {
                    item.Type3Name = basicTypes.FirstOrDefault(p => p.Id == item.Type3)?.Title ?? string.Empty;
                }
            });
            return ResultList.Success(count: result.Item2, data: map);
        }

        public async Task<Result> Del(string id)
        {
            return await _service.Delete(id, OperatorInfo) ? Result.Success("删除成功") : Result.Failed(-1, "删除失败");
        }

        public async Task<Result> Delist(List<string> ids)
        {
            return await _service.DeleteBatch(ids, OperatorInfo) ? Result.Success("删除成功") : Result.Failed(-1, "删除失败");
        }

        public async Task<IActionResult> Edit(string id)
        {
            var types = await _basicTypeService.GetList(new BasicTypeQueryDTO { Type = "EntryType" });
            ViewBag.Types = types;
            if (string.IsNullOrWhiteSpace(id))
            {
                var initModel = new EntryDetailDTO();
                if (types.Any())
                {
                    var types2 = await _basicTypeService.GetList(new BasicTypeQueryDTO { Type = "EntryType", ParentId = types.FirstOrDefault().Id });
                    ViewBag.Types2 = types2;
                    if (types2.Any())
                    {
                        ViewBag.Types3 = await _basicTypeService.GetList(new BasicTypeQueryDTO { Type = "EntryType", ParentId = types2.FirstOrDefault().Id });
                    }
                }
                return View(initModel);
            }
            else
            {
                var model = await _service.GetById(id);
                var modelDTO = _mapper.Map<EntryDetailDTO>(model);
                var types2 = await _basicTypeService.GetList(new BasicTypeQueryDTO { Type = "EntryType", ParentId = model.Type });
                ViewBag.Types2 = types2;
                if (types2.Any())
                {
                    ViewBag.Types3 = await _basicTypeService.GetList(new BasicTypeQueryDTO { Type = "EntryType", ParentId = model.Type2 });
                }
                return View(modelDTO);
            }
        }

        public async Task<Result> Save(EntryDetailDTO dto)
        {
            return await _service.Save(dto, OperatorInfo);
        }

        [NoLogin]
        public async Task<ResultList> GetList(EntryQueryDTO dto)
        {
            var result = await _service.GetPageList(dto);
            var map = _mapper.Map<List<EntryListDTO>>(result.Item1);
            var typeIds = map.Where(p => !string.IsNullOrWhiteSpace(p.Type)).Select(p => p.Type).ToList();
            typeIds.AddRange(map.Where(p => !string.IsNullOrWhiteSpace(p.Type2)).Select(p => p.Type2).ToList());
            typeIds.AddRange(map.Where(p => !string.IsNullOrWhiteSpace(p.Type3)).Select(p => p.Type3).ToList());
            var basicTypes = await _basicTypeService.GetByIds(typeIds);
            map.ForEach(item =>
            {
                if (!string.IsNullOrWhiteSpace(item.Type))
                {
                    item.TypeName = basicTypes.FirstOrDefault(p => p.Id == item.Type)?.Title ?? string.Empty;
                }
                if (!string.IsNullOrWhiteSpace(item.Type2))
                {
                    item.Type2Name = basicTypes.FirstOrDefault(p => p.Id == item.Type2)?.Title ?? string.Empty;
                }
                if (!string.IsNullOrWhiteSpace(item.Type3))
                {
                    item.Type3Name = basicTypes.FirstOrDefault(p => p.Id == item.Type3)?.Title ?? string.Empty;
                }
            });
            return ResultList.Success(count: result.Item2, data: map);
        }
    }
}